<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="formDate" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formDate.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="formDate.workNumber" size="mini" class="inputW" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="formDate.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <!-- .V-model="userInfo.departmentId":value @input  -->
                <!-- 2.子组件定义props。value-->
                <!-- 3。绑定给级联组件value替换v-model -->
                <!-- 监听级联组件的change事件，通过$emit（'input) -->
                <select-tree v-model="formDate.departmentId" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formDate.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formDate.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formDate.correctionTime"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像" prop="staffPhoto">
                <!-- 放置上传图片 -->
                <image-upload v-model="formDate.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="onUpdate">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { reviseEmployeeAPI, reviseListEmployeeAPI, updateEmployeeAPI } from '@/api/employee'
import selectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'

export default {
  components: { selectTree, ImageUpload },
  data() {
    return {
      formDate: {
        username: '',
        mobile: '',
        formOfEmployment: null,
        timeOfEntry: '',
        correctionTime: '',
        departmentId: 0,
        staffPhoto: ''

      },
      //   定义规则
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '请输选择', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            validator: (_, value, callback) => {
              // 判断入职时间存不存在,存在就判断转正时间是否大于入职时间
              if (this.formDate.timeOfEntry) {
                if (new Date(this.formDate.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                } else {
                  callback()
                }
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  async created() {
    // 判断是否有id 有id就是编辑 回填数据 没有id就是添加;
    // console.log(this.$route.params.id)
    if (!this.$route.params.id) return
    const res = await reviseEmployeeAPI(this.$route.params.id)
    // console.log(res)
    this.formDate = res.data
  },
  methods: {
    async onUpdate() {
      // 点击提交保存校验
      this.$refs.userForm.validate()
      // 判断是否有id 有id是编辑 没有则是新增
      if (!this.$route.params.id) {
        // 调接口 提示用户 刷新 跳转主页面
        await updateEmployeeAPI(this.formDate)
        this.$message.success('新增成功')
      } else {
        // 调接口 提示用户 刷新 跳转主页面
        await reviseListEmployeeAPI(this.formDate)
        this.$message.success('编辑成功')
      }

      this.$router.push('/employee')
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
